//
//  AlertMgr.m
//  CarCare
//
//  Created by ileo on 14-8-17.
//  Copyright (c) 2014年 baozun. All rights reserved.
//

#import "AlertMgr.h"

#import "BaseViewController.h"

#define GAP 8
#define TAG_BG 1217
#define TAG_LABEL 817

#define ALERT_WINDOW [AlertMgr sharedInstance].alertWindow
#define WAIT_WINDOW [AlertMgr sharedInstance].waitWindow
#define POPUP_WINDOW [AlertMgr sharedInstance].popupWindow
#define SHORT_WINDOW [AlertMgr sharedInstance].shortWindow

@interface AlertMgr()

@property (nonatomic, strong) UIWindow *waitWindow;
@property (nonatomic, strong) UIWindow *alertWindow;
@property (nonatomic, strong) UIWindow *popupWindow;
@property (nonatomic, strong) UIWindow *shortWindow;

@property (nonatomic, assign) BOOL isAlertShowing;

@end

@implementation AlertMgr

IMP_SINGLETON

- (instancetype)init
{
    self = [super init];
    if (self) {
    }
    return self;
}

+(void)showWaiting{
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 181 , 107)];
    view.center = WAIT_WINDOW.center;
    view.layer.cornerRadius = 2;
    view.tag = TYPE_VIEW_WAIT;
    view.alpha = 0.95;
    view.backgroundColor = COLOR_HEXSTRING(@"#2e2e2e");
    WAIT_WINDOW.hidden = NO;
    [WAIT_WINDOW makeKeyWindow];
    [WAIT_WINDOW addSubview:view];
    
    UIActivityIndicatorView *wait = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    wait.center = CGPointMake(view.width / 2, wait.frame.size.height / 2 + 17);

    [view addSubview:wait];
    [wait startAnimating];
    
    [Factory labelWithFrame:CGRectMake(10, wait.bottom + 15, view.width - 10 * 2, 19) font:FONT_DEFAULT_Light(16) text:@"努力加载中..." textColor:[UIColor whiteColor] onView:view textAlignment:NSTextAlignmentCenter];
    
    [view addAnimationLightenOutFinish:nil];
}

+(void)showView:(UIView *)view{

    if ([AlertMgr sharedInstance].isAlertShowing) {
        return;
    }
    
    [AlertMgr sharedInstance].isAlertShowing = YES;
    view.tag = TYPE_VIEW_ALERT;
    view.layer.cornerRadius = 2;
    ALERT_WINDOW.hidden = NO;
    [ALERT_WINDOW addSubview:view];
    view.center = ALERT_WINDOW.center;
    [view addAnimationLightenOutFinish:nil];
    [ALERT_WINDOW makeKeyAndVisible];
    
    UIView *bg = [ALERT_WINDOW viewWithTag:TAG_BG];
    [bg addAnimationFadeInToAlpha:bg.alpha Finish:nil];
}

//+(void)showViewAndExitGesture:(UIView *)view
//{
//    [AlertMgr showView:view];
//    UIView *bgg = [ALERT_WINDOW viewWithTag:TAG_BG];
//    bgg.userInteractionEnabled = YES;
//    UIButton * bgBtn = [Factory buttonWithFrame:bgg.bounds bgColor:[UIColor redColor] title:@"" textColor:[UIColor clearColor] click:^{
//        [AlertMgr dismiss:TYPE_VIEW_ALERT];
//    } onView:bgg];
//    [bgg bringSubviewToFront:bgBtn];
//
//}

+(void)showPopupView:(UIView *)view bottom:(CGFloat)bottom{
    view.tag = TYPE_VIEW_POPUP;
//    view.layer.cornerRadius = 10;
    POPUP_WINDOW.hidden = NO;
    [POPUP_WINDOW addSubview:view];
    view.center = POPUP_WINDOW.center;
    view.top = __MAIN_HEIGHT;
    [UIView animateWithDuration:0.3 animations:^{
        view.bottom = __MAIN_HEIGHT - bottom;
    }];
    [POPUP_WINDOW makeKeyAndVisible];
    UIView *bg = [POPUP_WINDOW viewWithTag:TAG_BG];
    [bg addAnimationFadeInToAlpha:bg.alpha Finish:nil];
}

+(void)showShortImage:(UIImage *)image Tips:(NSArray *)tips time:(NSInteger)time{
    UIWindow *mainWindow = SHORT_WINDOW;
    mainWindow.hidden = NO;
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
    view.layer.cornerRadius = 2;
    view.alpha = 0.95;
    view.backgroundColor = COLOR_HEXSTRING(@"#2e2e2e");
    [mainWindow addSubview:view];
    
    CGFloat height = GAP;
    CGFloat width = 149;
    
    UIImageView *imageV = nil;
    
    if (image) {
        imageV = [Factory imageViewWithOriginal:CGPointMake(0, height) image:image onView:view];
        height += imageV.height + GAP;
        if (imageV.width > width) width = imageV.width;
    }
    
    for (int i = 0; i < tips.count; i++) {
        UILabel *alabel = [ViewFactory labelWithMaxWidth:250 resetSizeFinish:nil text:tips[i] inView:view];
        alabel.top = height;
        alabel.tag = TAG_LABEL;
        height += alabel.height + 5;
        if (alabel.width > width) width = alabel.width;
        alabel.textColor = [UIColor whiteColor];
        alabel.font = FONT_DEFAULT_Bold(16);
    }
    
    view.width = width + 4*GAP;
    view.height = height + GAP;
    view.center = mainWindow.center;
    
    for (UIView *aView in view.subviews) {
        if (aView.tag == TAG_LABEL) {
            aView.centerX = view.width/2;
        }
    }
    
    if (imageV) {
        imageV.centerX = view.width/2;
    }else if (tips.count == 1 ) {
        for (UIView *aView in view.subviews) {
            if (aView.tag == TAG_LABEL) {
                aView.centerY = view.height/2 - 1;
            }
        }
    }
    
    [view addAnimationLightenOutFinish:nil];
    
    [NSTimer scheduledTimerWithTimeInterval:((time > 0)? time : (1 + 0.5*tips.count)) block:^{
        __weak __typeof(view) wView = view;
        [view addAnimationDieOutFinish:^{
            [wView removeFromSuperview];
            mainWindow.hidden = YES;
        }];
    } repeats:NO];
    
}

+(void)showShortImage:(UIImage *)image Tips:(NSArray *)tips {
    [AlertMgr showShortImage:image Tips:tips time:0];
}

+(void)showShortTextTips:(NSArray *)tips time:(NSInteger)time{
    [AlertMgr showShortImage:nil Tips:tips time:time];
}

+(void)showShortTextTips:(NSArray *)tips{
    [AlertMgr showShortImage:nil Tips:tips];
}

+(void)showShortSuccessTextTips:(NSArray *)tips{
    [AlertMgr showShortImage:ImageNamed(@"alert-succ") Tips:tips];
}
+(void)showShortWarnTextTips:(NSArray *)tips
{
    [AlertMgr showShortImage:ImageNamed(@"alert-warn") Tips:tips];
}
//+(void)showShortReloadTextTips:(NSArray *)tips
//{
//    [AlertMgr showShortImage:ImageNamed(@"alert-reload") Tips:tips];
//}
+(void)showShortNoNetworkTextTips:(NSArray *)tips
{
    [AlertMgr showShortImage:ImageNamed(@"alert-no-network") Tips:tips];
}
+(void)showShortNormalTextTips:(NSArray *)tips
{
    [AlertMgr showShortImage:ImageNamed(@"alert-normal") Tips:tips];
}
+(void)dismiss:(TYPE_VIEW)type{
    
    UIWindow *window;
    switch (type) {
        case TYPE_VIEW_ALERT:
            [AlertMgr sharedInstance].isAlertShowing = NO;
            window = ALERT_WINDOW;
            break;
            
        case TYPE_VIEW_WAIT:
            window = WAIT_WINDOW;
            break;
            
        case TYPE_VIEW_POPUP:
            window = POPUP_WINDOW;
            break;
        
        default:
            break;
    }
    
    UIWindow *mainWindow = [UIApplication sharedApplication].delegate.window;
    
    for (UIView *view in window.subviews) {
        if (view.tag == type) {
            
            [mainWindow addSubview:view];
            
            UIView *bg = [window viewWithTag:TAG_BG];
            __weak __typeof(bg) wBG = bg;
            window.hidden = YES;

            [bg addAnimationFadeOutFromAlpha:bg.alpha Finish:^{
                [wBG removeFromSuperview];
            }];
            
            if (TYPE_VIEW_POPUP == type) {
                [UIView animateWithDuration:0.6 animations:^{
                    view.top = __MAIN_HEIGHT;
                } completion:^(BOOL finished) {
                    [view removeFromSuperview];
                }];
            }else{

                __weak __typeof(view) wView = view;
                [view addAnimationDieOutFinish:^{
                    [wView removeFromSuperview];
                }];
            }

            break;
        }
    }
    
}

-(UIWindow *)waitWindow{
    if (!_waitWindow) {
        _waitWindow = [self createWindow];
    }
    return _waitWindow;
}

-(UIWindow *)shortWindow{
    if (!_shortWindow) {
        _shortWindow = [self createWindow];
    }
    return _shortWindow;
}

-(UIWindow *)alertWindow{
    if (!_alertWindow) {
        _alertWindow = [self createWindow];
    }
    if (![_alertWindow viewWithTag:TAG_BG]) {
        [_alertWindow addSubview:[self createBGViewColor:[UIColor blackColor] alpha:0.6]];
    }
    [_alertWindow viewWithTag:TAG_BG].alpha = 0.6;
    return _alertWindow;
}

-(UIWindow *)popupWindow{
    if (!_popupWindow) {
        _popupWindow = [self createWindow];
    }
    if (![_popupWindow viewWithTag:TAG_BG]) {
        UIView *bgView = [self createBGViewColor:[UIColor blackColor] alpha:0.6];
        [_popupWindow addSubview:bgView];
       	UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick)];
        [bgView addGestureRecognizer:tapRecognizer];
    }
    [_popupWindow viewWithTag:TAG_BG].alpha = 0.6;
    return _popupWindow;
}

-(UIWindow *)createWindow{
    UIWindow *window = [[UIWindow alloc] initWithFrame:__MAIN_BOUND];
    window.rootViewController = [[BaseViewController alloc] init];
    window.rootViewController.view.backgroundColor = [UIColor clearColor];
    [window.rootViewController.view endEditing:YES];
    NSArray * windows = [[UIApplication sharedApplication]windows];
    UIWindow * lastWindow = (UIWindow *)[windows lastObject];
    window.windowLevel = lastWindow.windowLevel + 1;
    window.backgroundColor = [UIColor clearColor];
    return window;
}

-(UIView *)createBGViewColor:(UIColor *)color alpha:(CGFloat)alpha{
    UIView *view = [[UIView alloc] initWithFrame:__MAIN_BOUND];
    view.backgroundColor = color;
    view.alpha = alpha;
    view.tag = TAG_BG;
    return view;
}

-(void)tapClick{
    [AlertMgr dismiss:TYPE_VIEW_POPUP];
}

@end
